package com.lzy.mall.ware.service.impl;

import cn.hutool.core.util.StrUtil;
import com.lzy.mall.common.utils.PageParaUtils;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.ware.feign.WmsMemberFeignService;
import com.lzy.mall.ware.entity.WmsWareInfo;
import com.lzy.mall.ware.dao.WmsWareInfoDao;
import com.lzy.mall.ware.service.WmsWareInfoService;
import com.lzy.mall.ware.vo.FareResponseVo;
import com.lzy.mall.ware.vo.MemberAddressVo;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * 仓库信息(WmsWareInfo)表服务实现类
 *
 * @author lzy
 * @since 2021-08-24 19:01:47
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class WmsWareInfoServiceImpl implements WmsWareInfoService {
    private final WmsWareInfoDao wareInfoDao;

    private final WmsMemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageQuery pageQuery = PageParaUtils.pagePara(params);

        LambdaQuery<WmsWareInfo> lambdaQuery = wareInfoDao.createLambdaQuery();

        String key = (String) params.get("key");
        if (StrUtil.isNotEmpty(key)){
            lambdaQuery.and(lambdaQuery.condition()
                    .andEq(WmsWareInfo::getId,key)
                    .orLike(WmsWareInfo::getName,"%" + key + "%")
                    .orLike(WmsWareInfo::getAddress,"%" + key + "%")
            );
        }

        PageQuery<WmsWareInfo> page = lambdaQuery.page(pageQuery.getPageNumber(), pageQuery.getPageSize());
        return new PageUtils(page);
    }

    @Override
    public void save(WmsWareInfo wareInfo) {
        wareInfoDao.insertTemplate(wareInfo);
    }

    @Override
    public void update(WmsWareInfo wareInfo) {
        wareInfoDao.updateTemplateById(wareInfo);
    }

    @Override
    public void removeBatch(List<Long> ids) {
        wareInfoDao.createLambdaQuery()
                .andIn(WmsWareInfo::getId,ids)
                .delete();
    }

    @Override
    public WmsWareInfo getById(Long id) {
        WmsWareInfo single = wareInfoDao.single(id);
        return single;
    }

    @Override
    public FareResponseVo getFare(Long addrId) {
        //远程查询运费
        ResultUtils info = memberFeignService.addrInfo(addrId);
        MemberAddressVo data = (MemberAddressVo) info.getBean(MemberAddressVo.class);
        System.out.println("远程调用数据"+data);

        if(data!=null){
            // TODO 可以结合第三方物流接口计算
            FareResponseVo fareResponseVo = new FareResponseVo();
            String phone = data.getPhone();
            String substring = phone.substring(phone.length() - 1, phone.length());
            System.out.println("运费" + substring);
            BigDecimal decimal = new BigDecimal(substring);
            fareResponseVo.setAddressVo(data);
            fareResponseVo.setFare(decimal);

            return fareResponseVo;
        }

        return null;
    }
}
